#define JMBC_INSIDE
#include "ilJPG.h"
#include "ilImage.h"
#include <Math/mlMath.h>
#include <Core/clCore.h>
#include <stdio.h>
#include <jpeglib.h>
#include <GL/glu.h>

void
ilImageLoadJPG(unsigned int id, unsigned int filepath)
{
	FILE *stream;
	char tmp[256];

	//stream = _wfopen(tlStringData(filepath), L"rb");
	wcstombs(tmp, tlStringData(filepath), 256);
	stream = fopen(tmp, "rb");

	if(stream)
	{
		ILImage *image;
		struct jpeg_decompress_struct decompress;
		struct jpeg_error_mgr error;
		unsigned char **rows;
		unsigned int i, row;

		image = ilImageStore(id);

		jpeg_create_decompress(&decompress);
		decompress.err = jpeg_std_error(&error);
		jpeg_stdio_src(&decompress, stream);
		jpeg_read_header(&decompress, TRUE);
		jpeg_start_decompress(&decompress);

		image->width = mlNextTwoPower((float) decompress.image_width);
		image->height = mlNextTwoPower((float) decompress.image_height);
		image->bpp = decompress.output_components;
		image->pixels = malloc(image->width * image->height * image->bpp * sizeof(unsigned char));

		rows = malloc(decompress.image_height * sizeof(unsigned char *));
		for(i = 0; i < decompress.image_height; i++)
			rows[decompress.image_height - 1 - i] = image->pixels + i * decompress.image_width * decompress.output_components;
		for(row = 0; decompress.output_scanline < decompress.output_height; row += jpeg_read_scanlines(&decompress, &rows[row], decompress.output_height - row));
		free(rows);

		jpeg_finish_decompress(&decompress);

		if(decompress.image_width != image->width || decompress.image_height != image->height)
			gluScaleImage(ilImageBPPToFormat(image->bpp), decompress.image_width, decompress.image_height, GL_UNSIGNED_BYTE, image->pixels, image->width, image->height, GL_UNSIGNED_BYTE, image->pixels);

		jpeg_destroy_decompress(&decompress);

		fclose(stream);
	}
	else
		elErrorSet(EL_ERROR_INVALID);
}

void
ilImageSaveJPG(unsigned int id, unsigned int filepath)
{
	FILE *stream;
	char tmp[256];

	//stream = _wfopen(tlStringData(filepath), L"wb");
	wcstombs(tmp, tlStringData(filepath), 256);
	stream = fopen(tmp, "wb");

	if(stream)
	{
		ILImage *image;
		struct jpeg_compress_struct compress;
		struct jpeg_error_mgr error;
		unsigned char **rows;
		unsigned int i, row;

		image = ilImageStore(id);

		jpeg_create_compress(&compress);
		compress.err = jpeg_std_error(&error);
		jpeg_stdio_dest(&compress, stream);
		compress.image_width = image->width;
		compress.image_height = image->height;
		compress.input_components = image->bpp;
		compress.in_color_space = JCS_RGB;
		jpeg_set_defaults(&compress);
		jpeg_start_compress(&compress, TRUE);

		rows = malloc(compress.image_height * sizeof(unsigned char *));
		for(i = 0; i < compress.image_height; i++)
			rows[compress.image_height - 1 - i] = image->pixels + i * image->width * image->bpp;
		for(row = 0; compress.next_scanline < compress.image_height; row += jpeg_write_scanlines(&compress, &rows[row], compress.image_height - row));
		free(rows);

		jpeg_finish_compress(&compress);
		jpeg_destroy_compress(&compress);

		fclose(stream);
	}
	else
		elErrorSet(EL_ERROR_INVALID);
}
